<?php

defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 用户组管理
 * @exception 400 参数错误
 * @exception 502 服务器错误
 * @exception 503 数据库错误
 */
class RoleController extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('Request');
        $this->load->model('Role');
    }

    /**
     * @return array
     */
    static function getRules()
    {
        return [
            'addRole' => [
                'name' => [
                    'name' => 'name', 'type' => 'string', 'max' => 20, 'require' => true, 'desc' => '名称(20字符内)', 'source' => 'post'
                ]
            ],
            'modRole' => [
                'id' => [
                    'name' => 'id', 'type' => 'int', 'require' => true, 'desc' => '用户组id', 'source' => 'post'
                ],
                'name' => [
                    'name' => 'name', 'type' => 'string', 'max' => 20, 'require' => true, 'desc' => '名称(20字符内)', 'source' => 'post'
                ]
            ],
            'modModule' => [
                'id' => [
                    'name' => 'id', 'type' => 'int', 'require' => true, 'desc' => '用户组id', 'source' => 'post'
                ],
                'moduleId' => [
                    'name' => 'moduleId', 'type' => 'array', 'format' => 'explode', 'separator' => ',', 'require' => false,'default'=>[], 'desc' => '功能模块id（多个以半角 , 分隔）', 'source' => 'post'
                ],
                'meid' => [
                    'name' => 'meid', 'type' => 'int', 'require' => true, 'desc' => '选中菜单ID', 'source' => 'post'
                ],
            ],
            'getModuleList' => [
                'meid' => [
                    'name' => 'meid', 'type' => 'int', 'require' => true, 'desc' => '选中菜单ID', 'source' => 'get'
                ],
                'rid' => [
                    'name' => 'rid', 'type' => 'int', 'require' => true, 'desc' => '用户组id', 'source' => 'get'
                ]
            ],
        ];
    }


    /**
     * 用户组
     * @desc 获取用户组列表
     * @return int id 用户组id
     * @return string name 用户组名称
     */
    public function getList()
    {
        $roleList = $this->db->select('id, name')->get('role')->result_array();

        restful(200, $roleList);
    }


    /**
     * 用户组
     * @desc 添加
     * @return int id 用户组id
     * @return string name 用户组名称
     * @throws MyException
     * @throws \Request\Exception\InternalServerErrorException
     */
    public function addRole()
    {
        $data = $this->request->getData();
        $result = $this->role->add($data['name']);

        if ($result !== false) {
            $this->optlog->write($result['id'],'添加角色'.$result['name'],optlog::ADDROLE);
            restful(200, $result);
        }else throw new MyException('', MyException::DATABASE);
    }


    /**
     * 用户组
     * @desc 修改（名称）
     * @throws MyException
     */
    public function modRole()
    {
        $data = $this->request->getData();
        $result = $this->role->mod($data['id'], $data['name']);
        if ($result !== false) {
            $this->optlog->write($data['id'],'修改角色'.$data['name'],optlog::MODROLE);
            restful(204, '操作成功');
        }else throw new MyException('', MyException::DATABASE);
    }


    /**
     * 用户组
     * @desc 删除
     * @throws MyException
     */
    public function delRole(int $id=0)
    {
        $result = $this->role->delete($id);
        if ($result !== false) {
            $info= $this->role->roleNameByID([$id]);
            $this->optlog->write($id,'删除角色'.$info,optlog::DELROLE);
            restful();
        }else throw new MyException('', MyException::DATABASE);
    }


    /**
     * 用户组
     * @desc 功能模块修改/添加
     * @throws MyException
     */
    public function modModule()
    {
        $data = $this->request->getData();
        $result = $this->role->modModule($data['id'], $data['moduleId'],$data['meid']);

        if ($result !== false) {
            restful();
        }else throw new MyException('', MyException::DATABASE);
    }

    /**
     * 用户组
     * @desc 获取用户组功能模块
     * @throws MyException
     */
    public function getModuleList()
    {
        $data = $this->request->getData();
        //得到所有菜单模块
        $allModuleList=$this->db->select('module.id, module.name')
            ->order_by("module.sort",'asc')
            ->where('module.mid', $data['meid'])
            ->get('module')->result_array();

        //得到当前菜单角色模块
        $menuList = $this->db->select('module.id')
            ->join('roleModule', 'roleModule.mid = module.id', 'left')
            ->order_by("module.sort",'asc')
            ->where('roleModule.rid', $data['rid'])
            ->where('module.mid', $data['meid'])
            ->get('module')->result_array();

        foreach($allModuleList as &$val){
            $val['checked']=0;
            foreach($menuList as $v){
                if($val['id']==$v['id']){
                    $val['checked']=1;
                    break;
                }
            }
        }

        restful(200, $allModuleList);
    }
}
